﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace chessLogic
{
    public class Board
    {
        private Figure[,] table;
        public Position OldPosition { get; set; }
        public Position NewPosition { get; set; }

        public Figure[,] Table
        {
            get { return this.table; }
            set { this.table = value; }
        }
        public Board()
        {
            this.table = new Figure[8, 8];
        }
        public void setFigure(Position position, Figure figure)
        {
            table[position.Number, position.GetCollNum()] = figure;
            figure.AddMovesMaked(position.ToString());
        }
        public Figure getFigure(Position position)
        {
            return table[position.Number, position.GetCollNum()];
        }
        public Position getPosition(int row, int col)
        { 
            string letters = "abcdefgh";
            char colChar = letters[col];
            return new Position(colChar, row);
        }
        public void InitialBoard()
        {
            for (int i = 2; i < 6; i++)
                for (int j = 0; j < 8; j++)
                    table[i, j] = new None();
            foreach(char ch in "abcdefgh")
            {
                setFigure(new Position(ch, 1), new Pawn(FigureColor.White));
                setFigure(new Position(ch, 6), new Pawn(FigureColor.Black));
            }
            setFigure(new Position('a', 0), new Rook(FigureColor.White));
            setFigure(new Position('h', 0), new Rook(FigureColor.White));
            setFigure(new Position('b', 0), new Knight(FigureColor.White));
            setFigure(new Position('g', 0), new Knight(FigureColor.White));
            setFigure(new Position('c', 0), new Bishop(FigureColor.White));
            setFigure(new Position('f', 0), new Bishop(FigureColor.White));
            setFigure(new Position('d', 0), new Queen(FigureColor.White));
            setFigure(new Position('e', 0), new King(FigureColor.White));
            setFigure(new Position('a', 7), new Rook(FigureColor.Black));
            setFigure(new Position('h', 7), new Rook(FigureColor.Black));
            setFigure(new Position('b', 7), new Knight(FigureColor.Black));
            setFigure(new Position('g', 7), new Knight(FigureColor.Black));
            setFigure(new Position('c', 7), new Bishop(FigureColor.Black));
            setFigure(new Position('f', 7), new Bishop(FigureColor.Black));
            setFigure(new Position('d', 7), new Queen(FigureColor.Black));
            setFigure(new Position('e', 7), new King(FigureColor.Black));
            OldPosition = null;
            NewPosition = null;
        }
        public void MoveFigure(Position old, Position _new)
        {
            setFigure(_new, getFigure(old));
            setFigure(old, new None());
            OldPosition = old;
            NewPosition = _new;
        }
        public bool IsPositionEmpty(Position position)
        {
            return getFigure(position).Type == FigureType.None;
        }
        public List<Position> getFigurePositions(Figure figure)
        {
            List<Position> retPos = new List<Position>();
            string letters = "abcdefgh";
            for (int i = 0; i < 8; i++)
                for (int j = 0; j < 8; j++)
                    if (table[i, j].Equals(figure))
                        retPos.Add(new Position(letters[j], i));
            return retPos;
        }
        
    }
}
